#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
const int NN = 1e5 + 4;
int T[NN];
struct LCA {
  static const int HH = 18;
  int timer = 0, Tin[NN], fa[HH][NN], Dep[NN];
  vi G[NN], W[NN];
  vector<pair<int, int>> X[NN];
  void addEdge(int u, int v) { G[u].push_back(v), G[v].push_back(u); }
  void dfs(int u, int f) {
    auto &w = W[T[u]];
    auto &x = X[T[u]];
    w.push_back(u), x.push_back({Tin[u] = timer++, u});
    fa[0][u] = f, Dep[u] = Dep[f] + 1;
    for (int k = 1; k < HH; ++k) fa[k][u] = fa[k - 1][fa[k - 1][u]];
    for (int v : G[u])
      if (v != f) dfs(v, u);
    w.pop_back(), x.push_back({timer, !w.empty() ? w.back() : 0});
  }
  int lca(int u, int v) {
    if (Dep[u] < Dep[v]) swap(u, v);
    for (int k = HH - 1; k >= 0; --k)
      if ((Dep[u] - Dep[v]) & (1 << k)) u = fa[k][u];
    for (int k = HH - 1; k >= 0; --k)
      if (fa[k][u] != fa[k][v]) u = fa[k][u], v = fa[k][v];
    return u == v ? u : fa[0][u];
  }
  int last(int u, int c) {
    auto &x = X[c];
    auto it = upper_bound(begin(x), end(x), make_pair(Tin[u], NN + 10));
    return it == begin(x) ? 0 : prev(it)->second;
  }
} L;
int solve(int u, int v, int c) {
  int d = L.lca(u, v), a = L.last(u, c);
  if (a && L.Dep[a] >= L.Dep[d]) return 1;
  a = L.last(v, c);
  return a && L.Dep[a] >= L.Dep[d];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N, M;
  cin >> N >> M;
  for (int i = 1; i <= N; ++i) cin >> T[i];
  for (int i = 1, u, v; i < N; ++i) cin >> u >> v, L.addEdge(u, v);
  L.dfs(1, 0);
  for (int i = 0, A, B, C; i < M; ++i)
    cin >> A >> B >> C, cout << solve(A, B, C);
  cout << endl;
}
